package com.android.home.fragment;

import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;
import com.android.common.base.BaseFragment;
import com.android.common.base.BaseFragmentAdapter;
import com.android.home.R;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import java.util.ArrayList;
import java.util.List;

public class HomeFirstFragment extends BaseFragment {

    public static HomeFirstFragment instance;

    private TabLayout mTabLayout;

    private ViewPager2 mViewPager;

    private List<Fragment> mFragments;

    private TabLayoutMediator mTabLayoutMediator;

    private final int[] tabTitle = {R.string.multi_select,R.string.wait_storage,R.string.storage
        ,R.string.exit_storage,R.string.wait_scrap,R.string.motion_layout,R.string.coordinator_layout};

    public static HomeFirstFragment getInstance(){
        if (null == instance){
            instance = new HomeFirstFragment();
        }
        return instance;
    }

    public HomeFirstFragment(){
    }

    @Override
    protected int onSetRootViewId() {
        return R.layout.common_tl_vp;
    }

    @Override
    protected void onFindView() {
        super.onFindView();
        mTabLayout = mFlRootContent.findViewById(R.id.tl_common);
        mViewPager = mFlRootContent.findViewById(R.id.vp_common);
    }

    @Override
    protected void onInitView() {
        super.onInitView();
        mTabLayoutMediator = new TabLayoutMediator(mTabLayout,mViewPager,true,false,(tab,position)->{
            tab.setText(getString(tabTitle[position]));
        });

        mFragments = new ArrayList<>();
        mFragments.add(new MultiSelectRvFragment());
        mFragments.add(new LayerListFragment());
        mFragments.add(new VPGroupFragment());
        mFragments.add(new VPTreeFragment());
        mFragments.add(new VPMediaSelectFragment());
        mFragments.add(new VPMotionLayoutFragment());
        mFragments.add(new VPCoordinatorLayoutFragment());
        BaseFragmentAdapter mFragmentStateAdapter = new BaseFragmentAdapter(this, mFragments);
        mViewPager.setAdapter(mFragmentStateAdapter);
        mViewPager.setUserInputEnabled(false);
        mTabLayoutMediator.attach();
    }


    @Override
    protected boolean onNeedBack() {
        return false;
    }

    @Override
    protected String onSetTitleText() {
        return "TabLayout + ViewPager2 + Fragment";
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        instance = null;
    }
}

